package com.xinyi.smart_translator.audioservice


import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.Service
import android.content.Intent
import android.media.MediaRecorder
import android.os.Build
import android.os.IBinder
import android.util.Log
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.core.app.NotificationCompat
import com.xinyi.smart_translator.R
import java.io.IOException


class AudioRecordingService : Service() {
    val NOTIFICATION_ID: Int = 1
    val CHANNEL_ID: String = "AudioRecordingChannel"

    var mediaRecorder: MediaRecorder? = null
    var outputFile: String? = null

    override fun onCreate() {
        super.onCreate()
        createNotificationChannel()
    }

    fun createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val serviceChannel = NotificationChannel(
                CHANNEL_ID,
                "Audio Recording Service",
                NotificationManager.IMPORTANCE_DEFAULT
            )
            val manager: NotificationManager = getSystemService(NotificationManager::class.java)
            manager.createNotificationChannel(serviceChannel)
        }
    }

    @RequiresApi(Build.VERSION_CODES.S)
    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        startRecording()

        val notification: Notification = NotificationCompat.Builder(this, CHANNEL_ID)
            .setContentTitle("Audio Recorder")
            .setContentText("Recording in progress")
            .setSmallIcon(R.drawable.ic_launcher)
            .build()

        startForeground(NOTIFICATION_ID, notification)
        return START_STICKY
    }


    @RequiresApi(Build.VERSION_CODES.S)
    private fun startRecording() {
        outputFile = getExternalCacheDir()?.getAbsolutePath();
        outputFile += "/recording_" + System.currentTimeMillis() + ".3gp";

        println("===========>  outputFile: ${outputFile}")
        Toast.makeText(this.applicationContext,outputFile, Toast.LENGTH_LONG).show();

        // 设置输出文件路径
        mediaRecorder = MediaRecorder(this.applicationContext)
        mediaRecorder!!.setAudioSource(MediaRecorder.AudioSource.MIC)
        mediaRecorder!!.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP)
        mediaRecorder!!.setOutputFile(outputFile)
        mediaRecorder!!.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB)

        try {
            mediaRecorder!!.prepare()
            mediaRecorder!!.start()
        } catch (e: IOException) {
            Log.e("AudioRecording", "prepare() failed")
        }
    }

    private fun stopRecording() {
        if (mediaRecorder != null) {
            mediaRecorder!!.stop()
            mediaRecorder!!.release()
            mediaRecorder = null
        }
    }

    public override fun onDestroy() {
        stopRecording()
        super.onDestroy()
    }

    public override fun onBind(intent: Intent?): IBinder? {
        return null
    }


}